/**
 * 
 */
package com.tx.horse.util;

import java.io.File;
import java.net.URL;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import com.tx.horse.HorseException;

/**
 * 
 * @author wanghy
 * @since 2013-4-3
 * 
 */
public class DomUtil {

	/**
	 * Get dom obj
	 * 
	 * @param filePath
	 * @return
	 * @throws Exception
	 */
	public static Document load(String filePath) throws HorseException {
		Document document = null;
		try {
			SAXReader saxReader = new SAXReader();
			URL url = ClassLoader.getSystemResource(filePath);
				document = saxReader.read(new File(url.getFile()));
		} catch (DocumentException e) {
			throw new HorseException(e);
		}
		return document;
	}
	
	/**
	 * Get xml root element
	 * 
	 * @throws Exception
	 */
	public static Element getRoot(String filePath) throws HorseException {
		return load(filePath).getRootElement();
	}
	
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		String fileName = "horse.cfg.xml";
		Document dom = DomUtil.load(fileName);
		Element root = dom.getRootElement();
		System.out.println(root.getName());
		
		Node node = dom.selectSingleNode(
				"/hibernate-configuration/session-factory/property[@name='hibernate.dialect']");
		System.out.println(node.getText());
//		for (Iterator<?> i = root.elementIterator(); i.hasNext();) {
//			Element el = (Element) i.next();
////			if(catNameEn.equals(el.elementTextTrim("engName"))){
////			flag = false;
////			break;
////			}
//			System.out.println(el.getName()+el.getStringValue());
//		}
	}

}
